package com.spzx.channel.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.IIteamService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductDetails;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.SkuPrice;
import com.spzx.product.api.domain.vo.SkuStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

@Service
public class IteamServiceImpl implements IIteamService {

    @Autowired
    private RemoteProductService remoteProductService;

    @Override
    public ItemVo item(Long skuId) {
        ItemVo itemVo = new ItemVo();

        //1.根据skuId查询ProductSku
        R<ProductSku> productSkuResult = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
        if(R.FAIL == productSkuResult.getCode()){
            throw new ServiceException(productSkuResult.getMsg());
        }
        ProductSku productSku = productSkuResult.getData();
        itemVo.setProductSku(productSku);

        //2.根据productId查询Product
        R<Product> productResult = remoteProductService.getProduct(productSku.getProductId(), SecurityConstants.INNER);
        if(R.FAIL == productResult.getCode()){
            throw new ServiceException(productResult.getMsg());
        }
        Product product = productResult.getData();
        itemVo.setProduct(product);
        //2.1 查询轮播图
        String sliderUrlsStr = product.getSliderUrls();
        String[] sliderUrlsStrArray = sliderUrlsStr.split(",");
        itemVo.setSliderUrlList(Arrays.asList(sliderUrlsStrArray));
        //2.2 获取商品规格信息
        //[{"key":"颜色","valueList":["白色","红色","黑色"]},{"key":"内存","valueList":["8G","18G"]}]
        String specValue = product.getSpecValue();
        JSONArray specValueJsonArray = JSON.parseArray(specValue);
        itemVo.setSpecValueList(specValueJsonArray);

        //3.根据skuId查询SkuPrice
        R<SkuPrice> skuPriceResult = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER);
        if(R.FAIL == skuPriceResult.getCode()){
            throw new ServiceException(skuPriceResult.getMsg());
        }
        SkuPrice skuPrice = skuPriceResult.getData();
        itemVo.setSkuPrice(skuPrice);

        //4.根据productId查询ProductDetails,可以获取到详情图片列表
        R<ProductDetails> productDetailsResult = remoteProductService
                .getProductDetails(product.getId(), SecurityConstants.INNER);
        if(R.FAIL == productDetailsResult.getCode()){
            throw new ServiceException(productDetailsResult.getMsg());
        }
        ProductDetails productDetails = productDetailsResult.getData();
        String imageUrls = productDetails.getImageUrls();
        String[] imageUrlsStrArray = imageUrls.split(",");
        itemVo.setDetailsImageUrlList(Arrays.asList(imageUrlsStrArray));

        //5.根据skuId查询SkuStockVo
        R<SkuStockVo> skuStockResult = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);
        if(R.FAIL == skuStockResult.getCode()){
            throw new ServiceException(skuPriceResult.getMsg());
        }
        SkuStockVo skuStockVo = skuStockResult.getData();
        itemVo.setSkuStockVo(skuStockVo);

        //6. 根据productId查询sku列表的数据，得到Map<String,Long>  规格对应skuId的Map集合
        R<Map<String, Long>> skuSpecValueMapResult = remoteProductService.getSkuSpecValue(product.getId(), SecurityConstants.INNER);
        if(R.FAIL == skuSpecValueMapResult.getCode()){
            throw new ServiceException(skuSpecValueMapResult.getMsg());
        }
        Map<String, Long> skuSpecValueMap = skuSpecValueMapResult.getData();
        itemVo.setSkuSpecValueMap(skuSpecValueMap);

        return itemVo;
    }
}
